var numEnclaves = function(grid) {
    let [ m, n] = [grid.length, grid[0].length];
    let data = new Map()
    let count = 0
    console.log(m,n)
    for( let r = 0;r < m; r++){
        for (let c =0; c< n ;c++ ){
        //   !c && console.log(r,c)
            /* 直接记录其行列信息，以便后面合并,还得是一个映射，后面要通过行列 找到 它在集合数据中的索引 */
         if( grid[r][c]===1) {

             data.set(r + ','+ c,count++ ) 
         } 
        }
    }

    let  uSet = new UnionSet(data, m,n) ;

   
    let data2 = [...data.entries()]
   for( const [str, ind] of data2){
      
    let [r,c] =str.split(',');
    r= Number(r)
    c= Number(c)
       /* 这个值算出来之后，就应该改变记录，因为初始化时没判断，现在才判断 */
        //   console.log(r,c, free1)
      
         if(false){
            debugger
        }
            /*  */
           if (grid[r][c]) {
            
                   if( c> 0 && grid[r][c-1]){
                       let b= data.get(r + ','+ (c-1)) ;
                      
                       uSet.merge(b, ind )
                   }
                   if(c< n-1&& grid[r][c+1]){
                       uSet.merge(data.get(r + ','+ (c+1)),ind  )
                   }
             

                   if( r> 0 && grid[r-1][c]){
                       uSet.merge(data.get(r-1 + ','+ c),   ind )
                   }
                //    console.log(r+1)
                   if(r< m-1&& grid[r+1][c]){
                       uSet.merge(data.get(r+1 + ','+ c),  ind )
                   }
           
           }  
           
    }

    console.log(uSet, data)
  let res =0
    for( const [str, ind] of data.entries()){
        let free = uSet.free[uSet.find(ind)]
        if(!free){
            console.log(  str)
                  res++
        }
    }
globalThis.uset = uSet ;

return res 

};

class UnionSet{
    constructor(data, m,n){
     
        let size = data.size ;
        this.boss = new Array(size+ 1).fill(0).map((v,i)=> i)
        this.free = [] ; // 记录是否联通外界
        for( const [str, ind] of data.entries()){
      
             let [r,c] =str.split(',');
             r= Number(r)
             c= Number(c)
             let cfree1 = ( c=== 0||  c === n-1) ;
             let rfree1 = (r ===0  || r === m-1) ;
             let  free1 = cfree1 || rfree1
             this.free.push( free1) ;
        }
        // this.count = new Array(n+1).fill(1)
        /* 还是得在初始化的时候就标注是否联通才比较好 */
    }
    merge(a,b){

        // console.log(afree,bfree)
   
        /* 这里直接把a 的树挂在了b下面， 这里就再加一个判断， 如果a 是边界，就让a成为老大，似乎比较麻烦，看了题解发现，只要再给集合加一个信息即可 */
        let [ba,bb] =  [this.find(a), this.find(b)]
        if(ba === bb){ return }
        this.boss[ba] = bb ;
        
        // this.count[bb]+=this.count[ba]/* 原来问题出在这里， 这个合并free也要取他们的根的free才行 */
        this.free[bb] = (this.free[bb] || this.free[ba] )
    }
    find(x){
        let boss =x ;
        while( this.boss[boss] !== boss){
            boss= this.boss[boss]
        }
        return this.boss[x] =  boss
        // return this.boss[x]= (this.boss[x] === x)?x: (this.find(this.boss[x]))
    }
    isFree(n){ return this.free[n]}
}
//  6,8 就是飞地 对应 38 结果51 ,合并的问题
let grid = [[0,0,0,1,1,1,0,1,0,0],[1,1,0,0,0,1,0,1,1,1],[0,0,0,1,1,1,0,1,0,0],[0,1,1,0,0,0,1,0,1,0],[0,1,1,1,1,1,0,0,1,0],[0,0,1,0,1,1,1,1,0,1],[0,1,1,0,0,0,1,1,1,1],[0,0,1,0,0,1,0,1,0,1],[1,0,1,0,1,1,0,0,0,0],[0,0,0,0,1,1,0,0,0,1]]
console.log(numEnclaves(grid))